package com.ac.api.group.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ac.api.group.dao.AcActionPageMapper;
import com.ac.api.group.entity.AcActionPage;
import com.ac.api.group.entity.AcGroupAction;
import com.ac.api.group.service.AcActionPageService;
import com.ac.api.group.service.AcGroupActionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author mzl
 * @since 2021-01-25
 */
@Service
public class AcActionPageServiceImpl extends ServiceImpl<AcActionPageMapper, AcActionPage> implements AcActionPageService {

    @Autowired
    private AcGroupActionService acGroupActionService;

    @Override
    public List<AcActionPage> list(AcActionPage example) {
        return this.list(buildWrapper(example));
    }

    @Override
    public IPage<AcActionPage> page(AcActionPage example, IPage page) {
        return this.page(page, buildWrapper(example));
    }

    @Override
    public List<String> findByGroupid(Integer groupid) {
        List<AcGroupAction> groupActions = acGroupActionService.findByGroupid(groupid);
        if (null != groupActions && groupActions.size() > 0) {
            //对groupActions列表进行流式操作(stream)，通过map函数将每个AcGroupAction对象映射为其对应的Action属性值，
            // 然后使用distinct函数去重，最后使用collect函数将映射后的结果收集到actions列表中。
            List<String> actions =
                    groupActions.stream().map(AcGroupAction::getAction).distinct().collect(Collectors.toList());
            List<AcActionPage> acActionPages = this.findByActions(actions);
            return acActionPages.stream().map(AcActionPage::getLink).collect(Collectors.toList());
        }
        return Collections.emptyList();
    }

    @Override
    public List<AcActionPage> findByActions(List<String> actions) {
        QueryWrapper<AcActionPage> wrapper = new QueryWrapper<>();
        wrapper.lambda().in(AcActionPage::getAction, actions)
                .eq(AcActionPage::getState, "1");
        return super.list(wrapper);
    }

    /**
     * 构建查询
     *
     * @param example
     * @return
     */
    private QueryWrapper<AcActionPage> buildWrapper(AcActionPage example) {
        QueryWrapper<AcActionPage> wrapper = new QueryWrapper<>();
        return wrapper;
    }

}
